<?php
/**
 +------------------------------------------------------------------------------
 * 商城管理--商品管理
 +------------------------------------------------------------------------------
 * @version   1.0
 * @since 2015-11-17
 +------------------------------------------------------------------------------
 */
namespace Admin\Controller;

class ShopGoodsController extends AdminController {
    /**
     * @todo 商品管理
     * @author fzh <fanzhaohai@hrbjjhh.com>
     */
	public function index(){
	    
	    if(IS_POST){
	        $promotion_start_time = (string)I('promotion_start_time','');
	        $promotion_end_time = (string)I('promotion_end_time','');
	        $ids = (array)I('ids','');
	         
	        $start = strtotime($promotion_start_time);
	        $end = strtotime($promotion_end_time);
	        if(empty($ids)){
	            $this->error('请选择要操作的数据');
	        }
	        if($start-$end>0){
	            $this->error('开始时间必须小于结束时间');
	        }
	        
	        $data['promotion_start_time'] = $start;
	        $data['promotion_end_time'] = $end;
	        $data['update_time'] = NOW_TIME;
	        $map['goods_id'] = array('in',$ids);
	        $id = M('ShopGoodsPromotion')->where($map)->save($data);
	        if(false == $id){
	            $this->error('编辑促销时间失败');
	        }else{
	            //促销信息缓存更新
	            S('sys_goods_promotion_list', null);
	            foreach ($ids as $goods_id){
    	            action_log('editSale', 'shopGoods', $goods_id, UID);
    	            S('shop_goods_promotion_stock_a_'.$goods_id,null);
    	            S('shop_goods_promotion__buy_stock_a_'.$goods_id,null);
	            }
	            $this->success('编辑促销时间成功');
	        }
	        
	    }
	    
	    //写的过于复杂～ 有问题  update by msj
	    $merchant_id = (int)I('get.merchant_id');  //商户id 
	    $shop_id = (int)I('get.shop_id');  //店铺id
	    $goods_category_pid = (int)I('get.goods_category_pid'); //一级品类id
	    $goods_category_id = (int)I('get.goods_category_id'); //二级品类id
	    $member_id = (int)I('get.member_id'); //创建人id
	    $status = (int)I('get.status','-1');    //状态 上架 下架
	    $goods_type = (int)I('get.goods_type','-1'); //类型
	    $goods_name = trim(I('get.goods_name','','htmlspecialchars')); //商品名称
	    $position = (int)I('get.position'); //商品名称
	    
	    if($merchant_id > 0){
	        $map['merchant_id'] = $merchant_id;
	    }
	    
	    if($shop_id > 0){
	        $map['shop_id'] = $shop_id;
	    }
	    
	    if($goods_category_pid > 0){
	        $map['goods_category_pid'] = $goods_category_pid;
	    }
	    
	    if($goods_category_id > 0){
	        $map['goods_category_id'] = $goods_category_id;
	    }
	    
	    if($member_id > 0){
	        $map['member_id'] = $member_id;
	    }
	    
	    if($status >= 0){
	        $map['status'] = $status;
	    }else{
	        $map['status']= array('gt',-1);
	    }
	    
	    if($goods_type >= 0){
	        $map['goods_type'] = $goods_type;
	    }
	    
	    if(!empty($goods_name)){
	        $map['goods_name'] =array('like','%'.$goods_name.'%');
	    }
	    
	    $order = 'update_time desc, sort desc, goods_id desc';
	    if(isset($_GET) && empty($_GET)){
	       $list = array();
	    }else{
	       $shop_goods = D('ShopGoods');
	       if($position > 0){
	           $maps = $shop_goods->listMap($position,$status);
	           $map = array_merge($map,$maps);
	       }
	       if($position == 0){
	           $map['position'] = $position;
	       }
	       $list=$this->lists($shop_goods,$map,$order);
	    }
	    int_to_string($list,array(
	        'goods_type'=>array('0'=>'正常','1'=>'促销'),
	        'status'=>array('-1'=>'删除','0'=>'未上架','1'=>'上架','2'=>'下架')
	    ));
        $this->assign('_list',$list);
        
        //所有商户
        $merchant_list = D('ServiceApi')->getMerchantList();
        $this->assign('merchant_list',$merchant_list);
        
	    //商户下的店铺
	    if($merchant_id >0){
	       $shop_list = D('ServiceApi')->getShopList($merchant_id);
	    }else{
	        $shop_list = null;
	    }
        $this->assign('shop_list',$shop_list);
        
        //店铺经营的所有一级品类
        $category_list = null;
        if($shop_id > 0){
            $goods_category = M('ShopGoodsCategoryShop')->where(array('shop_id'=>$shop_id))->field('goods_category_id')->select();
            if(!empty($goods_category)){
                $goods_category_id = array_column($goods_category,'goods_category_id');
                $category_list = D('ServiceApi')->getGoodsCategoryList($goods_category_id);
            }
        }
        $this->assign('category_list',$category_list);
        
        //一级品类下的二级品类
        if($goods_category_pid >0 ){
            $goods_category_list = D('ServiceApi')->getGoodsCategoryList($goods_category_pid);
        }else{
            $goods_category_list = null;
        }
        $this->assign('goods_category_list',$goods_category_list);
        
        //所有员工
        
        $member_list = D('ServiceApi')->getMemberList();
        $this->assign('member_list',$member_list);
        
        $this->assign('position',C('GOODS_POSITION'));

        cookie('__forward__',$_SERVER['REQUEST_URI']);
	    $this->assign('meta_title','商品管理');
	    $this->display('goods');
	}
	
	/**
     * @todo 添加商品
     * @author fzh <fanzhaohai@hrbjjhh.com>
     */
	public function add(){
	    if(IS_POST){
	        $tags = (array)I('tags','','htmlspecialchars');
	        $obj = D('ShopGoods');
	        $id = $obj->update();
	        if(false === $id){
	            $this->error($obj->getError());
	        }else{
	            //编辑标签
	            D('ShopGoods')->saveGoodsTags($id,$tags);
	            action_log('addShopGoods', 'shopGoods', $id, UID);
	            $this->success('新增成功', cookie('__forward__'));
	        }
	    } else {
	        //所有商户
	        $merchant_list = D('ServiceApi')->getMerchantList();
	        $this->assign('merchant_list',$merchant_list);
	        $this->assign('info',null);
	        $this->assign('tags',null);
	        $this->assign('position',C('GOODS_POSITION'));
	        $this->assign('meta_title','添加商品');
	        $this->display('edit');
	    }
	}
	
	/**
	 * @todo 修改商品信息
	 * @author fzh<fanzhaohai@hrbjjhh.com>
	 */
	public function edit(){
	    if(IS_POST){
	        $tags = (array)I('tags','','htmlspecialchars');
	        $goods_id = (int)I('goods_id');
	        $obj = D('ShopGoods');
	        $id = $obj->update();
	        if(false === $id){
	            $this->error($obj->getError());
	        } else {
	            $obj->saveGoodsTags($goods_id,$tags);//编辑标签
	            action_log('editShopGoods', 'shopGoods', $id, UID);
	            S('shop_goods_stock_a_'.$goods_id,null);
	            S('shop_goods_buy_stock_a_'.$goods_id,null);
	            $this->success('修改成功！', cookie('__forward__'));
	        }
	    } else {
	        $goods_id=(int)I('get.goods_id');
	        if($goods_id == 0){
	            $this->error('参数传递错误');
	        }
	        
	        $info = D('ShopGoods')->where(array('goods_id'=>$goods_id))->find();
	        if(empty($info)){
	            $this->error('商品信息不存在');
	        }
	        $this->assign('info',$info);
	        
	        //所有商户
	        $merchant_list = D('ServiceApi')->getMerchantList();
	        $this->assign('merchant_list',$merchant_list);
	        
	        //商户下的店铺
	        if(isset($info['merchant_id']) && $info['merchant_id']>0){
	            $shop_list = D('ServiceApi')->getShopList($info['merchant_id']);
	        }else{
	            $shop_list = null;
	        }
	        $this->assign('shop_list',$shop_list);
	        
	        //店铺经营的所有一级品类
	        $category_list = null;
	        if(isset($info['shop_id']) && $info['shop_id']>0){
	            $goods_category = M('ShopGoodsCategoryShop')->where(array('shop_id'=>$info['shop_id']))->field('goods_category_id')->select();
	            if(!empty($goods_category)){
	                $goods_category_id = array_column($goods_category,'goods_category_id');
	                $category_list = D('ServiceApi')->getGoodsCategoryList($goods_category_id);
	            }
	        }
	        $this->assign('category_list',$category_list);
	        
	        //一级品类下的二级品类
	        if(isset($info['goods_category_pid']) && $info['goods_category_pid']>0){
	            $goods_category_list = D('ServiceApi')->getGoodsCategoryList($info['goods_category_pid']);
	        }else{
	            $goods_category_list = null;
	        }
	        $this->assign('goods_category_list',$goods_category_list);
	        
	        $this->assign('position',C('GOODS_POSITION'));
	        //所有标签
	        $tags = D('ShopGoods')->getGoodsTags($goods_id);
	        $this->assign('tags',$tags);
	        
	        $this->assign('meta_title','编辑商品');
	        $this->display('edit');
	    }
	}
	
	/**
     * @todo 商品状态
     * @author fzh <fanzhaohai@hrbjjhh.com>
     */
	public function setGoodsStatus(){
	    $ids    =   (array)I('request.ids');
        $status =   (int)I('request.status');
        $model  =   I('request.model');
        $shop_goods = D('ShopGoods');
        $id = $shop_goods->setGoodsStatus($ids,$model,$status);
	    if(false === $id){
	        $this->error($shop_goods->getError());
	    }elseif ($id == 0){
	        $this->error('操作失败，必须先删除促销商品和团购商品之后，才能删除商品');
	    }else{
	        $info = '操作成功';
	        if($status == -1){
	            $info .= '，已删除正常类型商品';
	        }
	        foreach ($ids as $val){
	            switch ($status){
	                case 1:
	                    $action = 'onShopGoods';
	                    break;
	                case 2:
	                    $action = 'offShopGoods';
	                    break;
	                case -1:
	                    $action = 'deleteShopGoods';
	                    break;
	            }
	            action_log($action, 'shopGoods', $val, UID);
	        }
	        $this->success($info);
	    }
	}
	
	
	/**
	 * @todo 商品所有的标签
	 * @author fzh <fanzhaohai@hrbjjhh.com>
	 */
	public function getShopTags(){
	    return $this->ajaxReturn(M('ShopTags')->select());
	}
	
	/**
	 * @todo 添加商品
	 * @author fzh <fanzhaohai@hrbjjhh.com>
	 */
	public function saveTags(){
	    if(IS_POST){
	        $tags_name = I('tags_name','','htmlspecialchars');
	        $obj = D('ShopTags');
	        $id = $obj->update();
	        if(false === $id){
	            $data = array('status'=>0,'info'=>$obj->getError());
	        }else{
	        	//清除标签缓存
	        	S('sys_goods_tags_list', null);
	            $data = array('status'=>1,'info'=>'添加标签成功','data'=>array('tags_id'=>$id, 'tags_name'=>$tags_name));
	        }
	        $this->ajaxReturn($data);
	    }else{
	        $data = array('status'=>0,'info'=>'非法操作');
	        $this->ajaxReturn($data);
	    }
	}
	
	
	/**
	 * @todo 商品管理
	 * @author fzh <fanzhaohai@hrbjjhh.com>
	 */
	public function sale(){
	    
	    $merchant_id = (int)I('get.merchant_id');  //商户id
	    $shop_id = (int)I('get.shop_id');  //店铺id
	    $goods_category_pid = (int)I('get.goods_category_pid'); //一级品类id
	    $goods_category_id = (int)I('get.goods_category_id'); //二级品类id
	    $promotion_start_time = (int)I('get.promotion_start_time',0,'strtotime');
	    $promotion_end_time = (int)I('get.promotion_end_time',0,'strtotime');
	    
	    $map = array();
	    if($merchant_id > 0){
	        $map['merchant_id'] = $merchant_id;
	    }
	     
	    if($shop_id > 0){
	        $map['shop_id'] = $shop_id;
	    }
	     
	    if($goods_category_id > 0){
	        $map['goods_category_id'] = $goods_category_id;
	    }
	    
	    if($promotion_start_time > 0){
	        $map['promotion_start_time'] = array('gt',$promotion_start_time);
	    }
	    
	    if($promotion_end_time > 0){
	        $map['promotion_end_time'] = array('lt',$promotion_end_time);
	    }
	    
	    
        $map['status'] =array('gt',-1);
        $map['goods_type'] = 1;
        $order = 'sort desc ,goods_id desc';
        $field = 'status,goods_id,promotion_id,update_time,goods_name,goods_title,promotion_start_time,promotion_end_time,promotion_price,promotion_stock';
        if(isset($_GET) && empty($_GET)){
            $list = array();
        }else{
            $list=$this->lists(D('ShopGoodsPromotionView'),$map,$order,$field);
            int_to_string($list,array(
                'status'=>array('-1'=>'删除','0'=>'未上架','1'=>'上架','2'=>'下架')
            ));
        }
	    $this->assign('_list',$list);
	
	    //所有商户
	    $merchant_list = D('ServiceApi')->getMerchantList();
	    $this->assign('merchant_list',$merchant_list);
	
	    //商户下的店铺
	    if($merchant_id >0){
	       $shop_list = D('ServiceApi')->getShopList($merchant_id);
	    }else{
	        $shop_list = null;
	    }
        $this->assign('shop_list',$shop_list);
        
        //店铺经营的所有一级品类
        $category_list = null;
        if($shop_id > 0){
            $goods_category = M('ShopGoodsCategoryShop')->where(array('shop_id'=>$shop_id))->field('goods_category_id')->select();
            if(!empty($goods_category)){
                $goods_category_id = array_column($goods_category,'goods_category_id');
                $category_list = D('ServiceApi')->getGoodsCategoryList($goods_category_id);
            }
        }
        $this->assign('category_list',$category_list);
        
        //一级品类下的二级品类
        if($goods_category_pid >0 ){
            $goods_category_list = D('ServiceApi')->getGoodsCategoryList($goods_category_pid);
        }else{
            $goods_category_list = null;
        }
        $this->assign('goods_category_list',$goods_category_list);
	
	    //所有员工
	    $member_list = D('ServiceApi')->getMemberList();
	    $this->assign('member_list',$member_list);
	    //列表地址缓存
	    cookie('__forward__',$_SERVER['REQUEST_URI']);
	    $this->assign('meta_title','促销商品管理');
	    $this->display();
	}
	
	/**
	 * @todo 促销商品编辑
	 * @author fzh<fanzhaohai@hrbjjhh.com>
	 */
	public function editSale(){
	    $goods_id = (int)I('get.goods_id');
	    if($goods_id == 0){
	        $this->error('参数传递错误');
	    }
        
        //正常商品信息
        $info = D('ShopGoods')->field(true)->where(array('goods_id'=>$goods_id))->find();
	    if(empty($info)){
            $this->error('商品信息不存在');
	    }
	    //判断该商品是否可以设置团购 - fang update
	    if($info['goods_type'] != 0 && $info['goods_type'] != 1){
	        $this->error('该商品暂时无法设置促销信息');
	    }
	    if(IS_POST){
	        $promotion_start_time = I('promotion_start_time','0','strtotime');
	        $promotion_end_time = I('promotion_end_time','0','strtotime');
	        $goods_stock = (int)I('goods_stock');
	        $promotion_stock = (int)I('promotion_stock');
	        
	        if($promotion_end_time <= $promotion_start_time){
	            $this->error('结束时间必须要大于开始时间');
	        }
	        
	        if($info['goods_stock'] != $goods_stock){
	            $this->error('此商品正在销售中，不能设置为促销');
	        }
	        
	        if($info['goods_stock'] < $promotion_stock){
	            $this->error('促销库存必须小于正常销售商品的库存');
	        }
	        $obj = D('ShopGoodsPromotion');
	        $id = $obj->update();
	        if(false === $id){
	            $this->error($obj->getError());
	        } else {
	        	//促销信息缓存更新
	        	S('sys_goods_promotion_list', null);
	            //设置促销后再修改shop_goods的促销字段为1
	            D('ShopGoods')->where(array('goods_id'=>$goods_id))->save(array('goods_type'=>1));
	            action_log('editSale', 'shopGoods', $goods_id, UID);
	            S('shop_goods_promotion_stock_a_'.$goods_id,null);
	            S('shop_goods_promotion__buy_stock_a_'.$goods_id,null);
	            $this->success('促销商品编辑成功！', cookie('__forward__'));
	        }
	    } else {
	        $this->assign('info',$info);
	        
	        //获取促销商品的信息
	        $promotion_info = D('ShopGoodsPromotion')->where(array('goods_id'=>$goods_id))->find();
	        $this->assign('promotion_info',$promotion_info);
	        //所有商户
	        $merchant_list = D('ServiceApi')->getMerchantList();
	        $this->assign('merchant_list',$merchant_list);
	        
	        //商户下的店铺
	        if(isset($info['merchant_id']) && $info['merchant_id']>0){
	            $shop_list = D('ServiceApi')->getShopList($info['merchant_id']);
	        }else{
	            $shop_list = null;
	        }
	        $this->assign('shop_list',$shop_list);
	         
	        //店铺经营的所有一级品类
	        $category_list = null;
	        if(isset($info['shop_id']) && $info['shop_id']>0){
	            $goods_category = M('ShopGoodsCategoryShop')->where(array('shop_id'=>$info['shop_id']))->field('goods_category_id')->select();
	            if(!empty($goods_category)){
	                $goods_category_id = array_column($goods_category,'goods_category_id');
	                $category_list = D('ServiceApi')->getGoodsCategoryList($goods_category_id);
	            }
	        }
	        $this->assign('category_list',$category_list);
	         
	        //一级品类下的二级品类
	        if(isset($info['goods_category_pid']) && $info['goods_category_pid']>0){
	            $goods_category_list = D('ServiceApi')->getGoodsCategoryList($info['goods_category_pid']);
	        }else{
	            $goods_category_list = null;
	        }
	        $this->assign('goods_category_list',$goods_category_list);
	         
	        $this->assign('meta_title','编辑促销商品');
	        $this->display('editSale');
	    }
	}
	
	/**
	 * @todo 删除促销商品状态
	 * @author fzh <fanzhaohai@hrbjjhh.com>
	 */
	public function delPromotion(){
	    $ids    =   I('request.ids');
	    if(empty($ids)){
	        $this->error('请选择要操作的数据');
	    }
	    $shop_goods = D('ShopGoods');
	    $result = $shop_goods->delPromotion($ids);
	    if(false === $result){
	        $this->error($shop_goods->getError());
	    }elseif($result == 0){
	        $this->error('不是促销商品');
	    }else{
	        foreach ($ids as $val){
    	       action_log('delPromotion', 'shopGoods', $val, UID);
    	       S('shop_goods_stock_a_'.$val,null);
    	       S('shop_goods_buy_stock_a_'.$val,null);
    	       S('shop_goods_promotion_stock_a_'.$val,null);
    	       S('shop_goods_promotion_buy_stock_a_'.$val,null);
    	    }
	        $this->success('删除促销成功');
	    }
	}
	
	
	/**
	 * @todo   团购商品列表
	 * @author fang
	 */
	public function groupGoods(){
	    
	    //参数获取
	    $merchant_id = (int)I('get.merchant_id');  //商户id
	    $shop_id = (int)I('get.shop_id');  //店铺id
	    $goods_category_pid = (int)I('get.goods_category_pid'); //一级品类id
	    $goods_category_id = (int)I('get.goods_category_id'); //二级品类id
	    //条件获取
	    $list = null;
	    $map = array();
	    if($merchant_id > 0){
	        $map['merchant_id'] = $merchant_id;
	    }
	    if($shop_id > 0){
	        $map['shop_id'] = $shop_id;
	    }
	    
	    if($goods_category_id > 0){
	        $map['goods_category_id'] = $goods_category_id;
	    }
	    
	    //所有商户
	    $merchant_list = D('ServiceApi')->getMerchantList();
	    $this->assign('merchant_list',$merchant_list);
	    
	    //商户下的店铺
        $shop_list = ($merchant_id >0)? D('ServiceApi')->getShopList($merchant_id) : null ;
	    $this->assign('shop_list',$shop_list);
	    
	    //店铺经营的所有一级品类
	    $category_list = null;
	    if($shop_id > 0){
	        $goods_category = M('ShopGoodsCategoryShop')->where(array('shop_id'=>$shop_id))->field('goods_category_id')->select();
	        if(!empty($goods_category)){
	            $goods_category_id = array_column($goods_category,'goods_category_id');
	            $category_list = D('ServiceApi')->getGoodsCategoryList($goods_category_id);
	        }
	    }
	    $this->assign('category_list',$category_list);
	    
	    //一级品类下的二级品类
        $goods_category_list = ($goods_category_pid > 0)? D('ServiceApi')->getGoodsCategoryList($goods_category_pid) : null ;
	    $this->assign('goods_category_list',$goods_category_list);
	    
	    if(isset($_GET) && !empty($_GET)){
	        $map['status'] =array('gt', -1);
	        $map['goods_type'] = 2;//商品类型为团购
	        $order = 'sort,shop_group_id desc';
	        $list = $this->lists(D('ShopGroupView'),$map,$order,true);
	    }
        
	    //列表地址缓存
	    cookie('__forward__',$_SERVER['REQUEST_URI']);
	    $this->meta_title = '商品团购管理';
	    $this->assign('_list',$list);
	    $this->display('groupGoods');
	}
	
	/**
	 * @todo   删除商品团购配置
	 * @param  array   ids     团购id
	 * @todo   fang
	 */
	public function delGroup(){
	    
	    $ids = I('request.ids');
	    if(empty($ids)){
	        $this->error('请选择要操作的数据');
	    }
	    $obj = D('ShopGroup');
	    if(false === $obj->delGroup($ids)){
	        $this->error($obj->getError());
	    }else{
	        $this->success('取消团购成功');
	    }
	}
	
	/**
	 * @todo   编辑商品团购信息
	 * @param  array   post    商品的团购信息（新增或修改）
	 * @param  int     id      商品id
	 * @author fang
	 */
	public function editGroup(){

	    $obj = D('ShopGroup');
	    if(IS_POST){
	        //设置团购
	        $result = $obj->update();
	        if($result){
	            //商品状态修改
	            $goods_id = (int)I('post.goods_id');
	            $shop_group_id = (int)I('post.shop_group_id');
	            if($shop_group_id == 0){
	                $obj->setShopGoodsStatus($goods_id, 2);
	            }
	            action_log('editGroup', 'shopGroup', $goods_id, UID);
	            $this->success('设置成功', cookie('__forward__'));
	        }else{
	            $this->error($obj->getError());
	        }
	    }
	    $goods_id = (int)I('get.id');
	    if($goods_id == 0){
	        $this->error('参数错误');
	    }
	    $map['goods_id'] = $goods_id;
	    //判断该商品是否可以设置团购
	    $goods_info = $obj->getGoodsInfo($map);
	    if($goods_info['goods_type'] != 0 && $goods_info['goods_type'] != 2){
	        $this->error('该商品暂时无法设置团购信息');
	    }
	    //获取商品团购信息
	    $group_info = $obj->getGroupGoodsInfo($map);
	    
	    $this->meta_title = '编辑团购商品';
	    $this->assign('info', $goods_info);
	    $this->assign('group_info', $group_info);
	    $this->display('editGroup');
	    
	}
	
}
